function setInstancePreference(key, value) 
{
	widget.setPreferenceForKey(value, widget.identifier + "-" + key);
}

function setGlobalPreference(key, value)
{
	widget.setPreferenceForKey(value, key);
}

function setPrefixPreference(prefix, key, value)
{
	widget.setPreferenceForKey(value, prefix + '-' + key);
}

function getInstancePreference(key)
{
	return widget.preferenceForKey(widget.identifier + "-" + key);
}

function getGlobalPreference(key)
{
	return widget.preferenceForKey(key);
}

function getPrefixPreference(prefix, key)
{
	return widget.preferenceForKey(prefix + '-' + key);
}

// This file was generated by Dashcode from Apple Inc.
// You may edit this file to customize your Dashboard widget.

//
// Function: load()
// Called by HTML body element's onload event when the widget is ready to start
//
function load()
{
    setupParts();
	
	var debugBox = document.getElementById('debugBox');
	debugBox.value += widget.identifier + "\n";
	debugBox.value += getGlobalPreference("howRemoved");
	
	if (window.widget) {
		var waitTimeString = getInstancePreference("waitTime");
		if (waitTimeString && waitTimeString.length > 0 && !(/\D/.test(waitTimeString))) {
			var waitTime = document.getElementById("waitTime");
			waitTime.value = waitTimeString;
		}
		var waitUnitsString = getInstancePreference("waitUnits");
		if (waitUnitsString && waitUnitsString.length > 0 &&
			(waitUnitsString == '1000' || waitUnitsString == '60000' || 
			 waitUnitsString == '3600000')) {
				var waitUnits = document.getElementById("waitUnits");
				waitUnits.value = waitUnitsString;
		}
		var charsetLength = getInstancePreference("charsetLength");
		if (charsetLength && charsetLength.length > 0 && !(/\D/.test(charsetLength))) {
			for (var i = 1; i < charsetLength; i++) {
				add_charset(getInstancePreference("charset["+i+"]"));
			}
		} else {
			add_charset('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' + 
						'`~!@#$%^&*()_-+={}|[]\\:";\'<>?,./');
			add_charset('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789');
			add_charset('0123456789abcdef');
			add_charset('0123456789');
			add_charset('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz');
			add_charset('`~!@#$%^&*()_-+={}|[]\\:";\'<>?,./');
			save_charsets();
		}
		var charset = document.getElementById('characters');
		charset.object.setSelectedIndex(1);
	}
}

//
// Function: remove()
// Called when the widget has been removed from the Dashboard
//
function remove()
{
    // Stop any timers to prevent CPU usage
    // Remove any preferences as needed
	save_upgrade();
    setInstancePreference("waitTime", null);
	setInstancePreference("waitUnits", null);
	var length = getInstancePreference("charsetLength");
	if (length && length + 0 > 0) {
		for (var i = 1; i < length; i++) {
			setInstancePreference("charset["+i+"]", null);
		}
	}
	setInstancePreference("charsetLength", null);
	setGlobalPreference("lastIdent", widget.identifier);
}

//
// Function: hide()
// Called when the widget has been hidden
//
function hide()
{
    debugBox = document.getElementById('debugBox');
	debugBox.value += 'hidden\n';
}

//
// Function: show()
// Called when the widget has been shown
//
function show()
{
    debugBox = document.getElementById('debugBox');
	debugBox.value += 'shown\n';
}

//
// Function: sync()
// Called when the widget has been synchronized with .Mac
//
function sync()
{
    // Retrieve any preference values that you need to be synchronized here
    // Use this for an instance key's value:
    // instancePreferenceValue = widget.preferenceForKey(null, createInstancePreferenceKey("your-key"));
    //
    // Or this for global key's value:
    // globalPreferenceValue = widget.preferenceForKey(null, "your-key");
}

//
// Function: showBack(event)
// Called when the info button is clicked to show the back of the widget
//
// event: onClick event from the info button
//
function showBack(event)
{
    var front = document.getElementById("front");
    var back = document.getElementById("back");

    if (window.widget) {
        widget.prepareForTransition("ToBack");
    }

    front.style.display = "none";
    back.style.display = "block";

    if (window.widget) {
        setTimeout('widget.performTransition();', 0);
    }
}

//
// Function: showFront(event)
// Called when the done button is clicked from the back of the widget
//
// event: onClick event from the done button
//
function showFront(event)
{
    var front = document.getElementById("front");
    var back = document.getElementById("back");

    if (window.widget) {
        widget.prepareForTransition("ToFront");
    }

    front.style.display="block";
    back.style.display="none";

    if (window.widget) {
        setTimeout('widget.performTransition();', 0);
    }
}

if (window.widget) {
    widget.onremove = remove;
    widget.onhide = hide;
    widget.onshow = show;
    widget.onsync = sync;
}

function usel33t_change(event) {
	// Values you provide
	var popupValue = document.getElementById("usel33t");	// replace with ID of popup

	// Popup code
	popupValue = popupValue.object.getValue();

	// Values you provide
	var buttonToDisable = document.getElementById("l33tlevel");	// replace with ID of button

	// Button code
	buttonToDisable.object.setEnabled(popupValue != 'usel33t_no');
	go_click(event);
}


function go_click(event)
{
    preGeneratePassword();
	var gauge = document.getElementById("strength");
	
	gauge.object.setValue(getPasswordStrength());
	nextClearTime = new Date();
	nextClearTime = nextClearTime.getTime();
	var units = document.getElementById("waitUnits");
	units = units.value;
	var wait = document.getElementById("waitTime");
	wait = wait.value;
	time = wait * units;
	nextClearTime = nextClearTime + time;
	setTimeout('maybe_clear_master()', time);
}


function length_change(event)
{
    var length = document.getElementById("length");
	if (/\D/.test(length.value)) length.value = "8";

    go_click(event);
}

var nextClearTime = 0;
function maybe_clear_master()
{
	var now = new Date();
	now = now.getTime();
	if (now >= nextClearTime) {
		masterpass = document.getElementById('masterpass');
		masterpass.value = '';
		genpass = document.getElementById('genpasswd');
		genpass.value = '';
	}
}

function change_waitTime(event)
{
    if (window.widget) {
		var waitTime = document.getElementById("waitTime");
		waitTime = waitTime.value;
		setInstancePreference("waitTime", waitTime);
	}
}

function change_waitUnits(event)
{
    if (window.widget) {
		var waitUnits = document.getElementById("waitUnits");
		waitUnits = waitUnits.value;
		setInstancePreference("waitUnits", waitUnits);
	}
}


function chars_change(event)
{
    var charset = document.getElementById("characters");
	var charsetText = charset.object.getValue();
	if (charsetText != '{EDIT}') {
		go_click(event);
	} else {
		var chars_edit = document.getElementById("chars_edit");
		charset.style.visibility='hidden';
		chars_edit.style.visibility='visible';
	}
}

function add_charset(chars)
{
	if (!chars) return;
	var charset = document.getElementById("characters");
	var newOption = document.createElement("OPTION");
	newOption.text = chars;
	newOption.value = chars;
	charset.object.select.add(newOption);
}

function save_charsets()
{
	save_prefix_charsets(widget.identifier);
}

function save_prefix_charsets(prefix)
{
	var charset = document.getElementById("characters");
	var length = charset.object.select.length;
	setPrefixPreference(prefix, "charsetLength", length);
	for (var i = 1; i < length; i++) {
		setPrefixPreference(prefix, "charset["+i+"]", charset.object.select.options[i].value);
	}
}

function save_upgrade()
{
	save_prefix_charsets('upgrade');
	setPrefixPreference('upgrade', 'removeTime', new Date().getTime());
}

function chars_edited(event)
{
	var charset = document.getElementById("characters");
    var chars_edit = document.getElementById("chars_edit");
	
	if (chars_edit.style.visibility == 'visible') {
		charset.style.visibility = 'visible';
		chars_edit.style.visibility = 'hidden';
		// set the option element text and value attributes
		add_charset(chars_edit.value);
		charset.object.setSelectedIndex(charset.object.select.length - 1);
		chars_edit.value = '';
		// save preference
		save_charsets();
		go_click(event);
	}
}
